<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\logic\notice\NoticeLogic;
use app\common\service\Util;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Notice extends Base
{
    public function index()
    {
        return View::fetch();
    }

    public function data()
    {
        $param = Request::get();
        $where = [];
        $data = Db::name('notice')
            ->where($where)
            ->order('id desc')
            ->paginate($param['limit']);
        $items = $data->items();
        if (count($items)) {
            foreach ($items as &$value) {
                $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
            }
        }
        return $this->genTableJson($data, $items);
    }

    public function add()
    {
        $request = Request::instance();
        if ($request->isPost()) {
            $post = $request->post();
            if (isset($post['id']) && $post['id']) {
                Db::name('notice')->where('id', $post['id'])->update($post);
            } else {
                Db::name('notice')->insert($post);
            }
            $this->successJump('success', url('notice/index'));
        }

        return View::fetch();
    }

    public function detail()
    {
        $param = Request::get();
        $shipteyue = Db::name('notice')->find($param['id']);
        View::assign('shipteyue', $shipteyue);
        return View::fetch();
    }

    public function edit()
    {
        $param = Request::get();
        $shipteyue = Db::name('notice')->find($param['id']);
        View::assign('shipteyue', $shipteyue);
        return View::fetch();
    }

    public function del()
    {
        $shipteyue_id = Request::post('id');
        Db::name('notice')
            ->where('id', $shipteyue_id)
            ->delete();
        $this->successJump('删除成功', url('notice/index'));
    }

    public function nums()
    {
        Util::ajaxReturn(1, 'ok:', NoticeLogic::getNums());
    }
}
